<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="twitter4j.Twitter"%>
<%@ page import="twitter4j.TwitterFactory"%>
<%@ page import="twitter4j.TwitterException"%>
<%@ page import="twitter4j.ResponseList"%>
<%@ page import="twitter4j.Status"%>
<%@ page import="twitter4j.http.RequestToken"%>
<%@ page import="java.text.DateFormat"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.TimeZone"%>

<%@ page import="mytweetdiary.ds.*"%>
<%@ page import="mytweetdiary.*"%>

<%
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
%>

<html>
  <head>
  	<title>My Tweet Diary</title>
    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
  </head>
<body>
<div class="body_wrapper">
	<div class="header">
	<% if (user!=null) {%>
		<div class="user"> 
			Hello, <%=user.getNickname()%>! 
			[<a href="<%=userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>]
		</div>
	<% 	} %>
	</div>
	<div class="contents">
	<%	
		// Needs Google Login 
		if (user == null) { 
	%>
			<p class="center"> 
				<br/><br/> <strong>Hello!</strong><br/><br/>  
				<a href="<%=userService.createLoginURL(request.getRequestURI())%>">Sign	in</a> to use me.
			</p>
	<%
		}
		else {		
			TwitterManager tm = new TwitterManager(user);
			if( tm.isAuthorized() == true) {				
			// show Diary
			Twitter twitter = tm.getTwitterInstance();
			
			try {
				ResponseList<Status> statuses = twitter.getUserTimeline();
				DateFormat dfm = new SimpleDateFormat("MM/dd/yyyy");
				dfm.setTimeZone(TimeZone.getTimeZone("Asia/Seoul"));
				String currentDate = "";
			    for (Status status : statuses) {
			    	String date = dfm.format(status.getCreatedAt());
			    	if (currentDate.compareTo(date) != 0) {
			    		currentDate = date;
			    		out.println("<h4>" + date + "</h4>");
			    	}
			        out.println("<p>" + status.getText() + "</p>");
			    }
			}
			catch(TwitterException te) {
				if(te.getStatusCode() == 401) {
					// unauthorized.. cleear access token info from datastore and do authorization again.
					tm.clearTwitterInfo();
					RequestToken requestToken = twitter.getOAuthRequestToken();
					tm.saveRequestToken(requestToken);
	%>
						<p class="center">
							<br/><h4>Needs Twitter Authorization</h4>
							<a href="<%= requestToken.getAuthorizationURL() %>">authorize me</a>
						</p>	
	<%						
					}
					
				}
			}
			else {
				// need Twiter Authorization.
				Twitter twitter = tm.getTwitterInstance();
				RequestToken requestToken = twitter.getOAuthRequestToken();
				tm.saveRequestToken(requestToken);
	%>
				<p class="center">
					<br/><h4>Needs Twitter Authorization</h4>
					<a href="<%= requestToken.getAuthorizationURL() %>">authorize me</a>
				</p>	
	<%		}
		}
	%>
	</div>	
</div>
</body>
</html>
